//
//  MyLevelView.m
//  ClassifyShop
//
//  Created by yuyao on 15/2/25.
//  Copyright (c) 2015年 yuyao. All rights reserved.
//

#import "MyLevelView.h"
#import "GUIDefine.h"


@implementation MyLevelView
@synthesize mydelegate,viewArray;

- (id)initWithFrame:(CGRect)frame andViews:(NSArray *)views{
    self = [super initWithFrame:frame];
    
    if (self) {

        CATransform3D transform = CATransform3DIdentity;
        
        transform.m34 = -1.0/1000.0;

        self.layer.sublayerTransform = transform;
        
        viewArray = [NSMutableArray arrayWithArray:views];
        
        [self addOrRemoveView:nil Boo:YES];
    }
    
    return self;
}
- (void)addOrRemoveView:(MyView *)myview Boo:(BOOL)boo{//yes 加 no 删
    NSMutableArray *array =  [NSMutableArray arrayWithArray:[appDelegate getTags]];

    NSComparator cmptr = ^(id obj1, id obj2){
        
        MyView *obj11 = (MyView *)obj1;
        MyView *obj22 = (MyView *)obj2;
        
        if ([obj11 viewId] > [obj22 viewId]) {
            return (NSComparisonResult)NSOrderedDescending;
        }
        
        if ([obj11 viewId] < [obj22 viewId]) {
            return (NSComparisonResult)NSOrderedAscending;
        }
        return (NSComparisonResult)NSOrderedSame;
    };
    if (myview) {
        if (boo) {
            [viewArray addObject:myview];
            [myview showRemoveButton:YES];
            [myview setFrame:CGRectMake(leftPadding, self.frame.size.height, self.frame.size.width-leftPadding*2, viewHeight)];
            
            [array addObject:[NSNumber numberWithInt:myview.viewId]];
            [appDelegate setTags:array];
            
            NSArray *array = [viewArray sortedArrayUsingComparator:cmptr];
            
            for (int i = 0;i<array.count;i++) {
                MyView *view = (MyView *)[array objectAtIndex:i];
                
                [view setTag:100+i];
                
                
            }
            
            
        }else{
            
            [viewArray removeObject:myview];
            
            [array removeObject:[NSNumber numberWithInt:myview.viewId]];
            
            [appDelegate setTags:array];

            
            for (int i = 0;i<viewArray.count;i++) {
                MyView *view = (MyView *)[viewArray objectAtIndex:i];
                if (view.tag>myview.tag) {
                    [view setTag:view.tag-1];
                }
                
            }
            
        }
  
    }
    

    for (int i =0; i<viewArray.count; i++) {
        
        MyView *subview  = (MyView *)[viewArray objectAtIndex:i];
        [subview setBackgroundColor:[UIColor whiteColor]];
        
        [self addSubview:subview];
        if (i==0) {
            
            [subview setFrame:CGRectMake(leftPadding, topwidth, self.frame.size.width-leftPadding*2, 180)];
            
            subview.layer.transform = CATransform3DMakeRotation(0, 1, 0, 0);
            
            
        }else{
            if (myview) {//后来添加 需要动画
                
                [UIView animateWithDuration:animationTime animations:^{
                     //点击第一个  后边的距离
                    [subview setFrame:CGRectMake(leftPadding, topwidth+firstWidth+((self.frame.size.height-topwidth-64-firstWidth)/viewArray.count)*i, self.frame.size.width-leftPadding*2, viewHeight)];

                }];
                
                subview.layer.transform = CATransform3DMakeRotation(-M_PI/8, 1, 0, 0);
                [subview setIsLean:YES];
            }else{
                
                 //点击第一个  后边的距离
                [subview setFrame:CGRectMake(leftPadding, topwidth+firstWidth+((self.frame.size.height-topwidth-64-firstWidth)/viewArray.count)*i, self.frame.size.width-leftPadding*2, viewHeight)];

                subview.layer.transform = CATransform3DMakeRotation(-M_PI/8, 1, 0, 0);
                [subview setIsLean:YES];

            }

        }
        
        [subview.layer setShadowOpacity:0.4];
        [subview.layer setShadowOffset:CGSizeMake(0.f,-.2f)];
        
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapped:)];
        [subview addGestureRecognizer:tap];
        
        UILongPressGestureRecognizer * longPressGr = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressToDo:)];
        [subview addGestureRecognizer:longPressGr];
        
        
        UISwipeGestureRecognizer *recognizerUp = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeFrom:)];
        
        [recognizerUp setDirection:(UISwipeGestureRecognizerDirectionUp)];
        [subview addGestureRecognizer:recognizerUp];

        
       UISwipeGestureRecognizer *recognizerDown = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeFrom:)];
        [recognizerDown setDirection:(UISwipeGestureRecognizerDirectionDown)];
        [subview addGestureRecognizer:recognizerDown];
        

        
  //      MyView *lastView = (MyView *)[self viewWithTag:100+viewArray.count-1];
 
    //    [self setContentSize:CGSizeMake(self.frame.size.width,lastView.frame.origin.y+viewHeight/2)];
        [self setContentSize:CGSizeMake(self.frame.size.width,self.frame.size.height)];

    }

}
- (void)handleSwipeFrom:(UISwipeGestureRecognizer *)swipRec{

    
    
    MyView *myView = (MyView *)[swipRec view];
    int tag1 =(int)myView.tag;

    int tag = 0;
    
    if ([swipRec direction]== UISwipeGestureRecognizerDirectionDown) {
        
        if (tag1==100) {
            tag = tag1;
        }else{
            if (myView.isLean) {
                
                 tag = tag1;
            }else{
                
                 tag = tag1-1;
            }
           
        }
    }
    if ([swipRec direction]== UISwipeGestureRecognizerDirectionUp) {
        
        if (tag1==viewArray.count+100-1) {
            tag = tag1;
        }else{
            if (myView.isLean) {
                
                tag = tag1;
            }else{
                
                tag = tag1+1;
            }
        }
    }

    MyView *my = (MyView *)[self viewWithTag:tag];
    
    [self tapped:my];
    
}
#pragma mark - gesture recognizer
- (void)tapped:(id)sender
{
    
    MyView *myView = nil;
    
    if ([sender isKindOfClass:[UIView class]]) {
        myView = (MyView *)sender;
    }else{
        myView = (MyView *)[sender view];
    }
    
    
    
    int tag =(int)myView.tag;
    
    [mydelegate selectedViewWithIndex:tag];
    
    [myView setLean:NO];
    
    for (UIView *view1 in self.viewArray) {
        
        
        MyView *view  = (MyView *)view1;
        
        if (view.tag!=tag) {
            
            
            if (view.tag<tag) {
                view.layer.anchorPoint = CGPointMake(0, 0);
                
                if (view.tag-100==0) {
                    
                    [view setFrame:CGRectMake(leftPadding, topwidth, self.frame.size.width-leftPadding*2, 180)];
                    
                }else{
                    if (view.isUp) {
                        
                        [view setFrame:CGRectMake(leftPadding, topwidth+viewPadding*(view.tag-100), self.frame.size.width-leftPadding*2, viewHeight)];
                        
                    }else{
                        //点击第一个  后边的距离
                        [view setFrame:CGRectMake(leftPadding, topwidth+firstWidth+((self.frame.size.height-topwidth-64-firstWidth)/viewArray.count)*(view.tag-100), self.frame.size.width-leftPadding*2, viewHeight)];
                        
                    }
                }
                
                [UIView animateWithDuration:animationTime animations:^{
                    
                    view.layer.anchorPoint = CGPointMake(0, 0);
                    
                    [view setFrame:CGRectMake(leftPadding, topwidth+viewPadding*(view.tag-100), self.frame.size.width-leftPadding*2, viewHeight)];
                    
                }];
                
                view.layer.transform = CATransform3DMakeRotation(-M_PI/8, 1, 0, 0);
                
                [view setIsUp:YES];
            }else{
                
                view.layer.anchorPoint = CGPointMake(0.5, 0.5);
                
                
                if (view.isUp) {
                    
                    [view setFrame:CGRectMake(leftPadding, topwidth+viewPadding*(view.tag-100), self.frame.size.width-leftPadding*2, viewHeight)];
                    
                }
                
                [UIView animateWithDuration:animationTime animations:^{
                    if (myView.tag==100) {
                        //点击第一个  后边的距离
                        [view setFrame:CGRectMake(leftPadding, topwidth+firstWidth+((self.frame.size.height-topwidth-64-firstWidth)/viewArray.count)*(view.tag-100), self.frame.size.width-leftPadding*2, viewHeight)];
                        
                    }else{
                        
                        //   [view setFrame:CGRectMake(leftPadding, self.frame.size.height -74- viewPadding*(self.subviews.count-(view.tag-100)), self.frame.size.width-leftPadding*2, viewHeight)];
                        
                        [view setFrame:CGRectMake(leftPadding,viewHeight+viewPadding*((view.tag-100-1))-40, self.frame.size.width-leftPadding*2, viewHeight)];
                        
                    }
                    
                    
                }];
                
                [(MyView *)view setLean:YES];
                [view setIsUp:NO];
            }
            
        }else{
            
            if (view.tag-100==0) {
                view.layer.anchorPoint = CGPointMake(0, 0);
                
                [view setFrame:CGRectMake(leftPadding, topwidth, self.frame.size.width-leftPadding*2, 180)];
                
            }
            if (view.tag>100) {
                
                
                [UIView animateWithDuration:animationTime animations:^{
                    
                    [myView setFrame:CGRectMake(leftPadding, topwidth+viewPadding*(myView.tag-100), self.frame.size.width-leftPadding*2, viewHeight)];
                    
                    
                } ];
                
                [self performSelector:@selector(resizeView:) withObject:myView afterDelay:animationTime];
                
                // [self resizeView:myView];
                
                
            }
            
            
            
            [view setIsUp:YES];
        }
        
        
    }
    //   MyView *lastView = (MyView *)[self viewWithTag:100+viewArray.count-1];
    
    //    if (myView.tag==lastView.tag) {
    //
    //        [self setContentSize:CGSizeMake(320,lastView.frame.origin.y+viewHeight+70)];
    //
    //
    //    }else{
    //
    //        [self setContentSize:CGSizeMake(320,lastView.frame.origin.y+viewHeight/2)];
    //
    //    }
    [self setContentSize:CGSizeMake(self.frame.size.width,self.frame.size.height)];
    
}
- (void)resizeView:(MyView *)myV{
 
        for (UIView *view11 in self.subviews) {
            if (view11.tag<myV.tag) {
                [UIView animateWithDuration:animationTime animations:^{
                
                    [view11 setFrame:CGRectMake(leftPadding, topwidth+viewPadding*(view11.tag-100), self.frame.size.width-leftPadding*2, viewHeight-150)];
                } ];

                
            }
        }
    

}
-(void)longPressToDo:(UILongPressGestureRecognizer *)gesture
{
    if (gesture.state==UIGestureRecognizerStateBegan) {
        
        [mydelegate editingModal];
        
        for (UIView *view1 in self.subviews) {
            
            if ([(MyView *)view1 respondsToSelector:@selector(showRemoveButton:)]) {
                
                [(MyView *)view1 showRemoveButton:YES];
                
            }
            
        }
    }

}
-(void)endEditting{

        for (UIView *view1 in self.subviews) {

            if ([(MyView *)view1 respondsToSelector:@selector(showRemoveButton:)]) {
                
                [(MyView *)view1 showRemoveButton:NO];

            }
            
            
        }
    
    
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
